package com.xm.runtimetask.util;

import android.app.Application;
import android.os.AsyncTask;

import java.io.DataOutputStream;
import java.io.OutputStream;

public class CmdUtil {

    /**
     * 执行shell命令
     *
     * @param cmd
     */
    public static void execShellCmd(String cmd) {
        try {
            // 申请获取root权限，这一步很重要，不然会没有作用
            Process process = Runtime.getRuntime().exec("su");
            // 获取输出流
            OutputStream outputStream = process.getOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(
                    outputStream);
            dataOutputStream.writeBytes(cmd);
            dataOutputStream.flush();
            dataOutputStream.close();
            outputStream.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    /**
     * 执行home键命令
     */
    public static void execHomeKeyCmd() {
        CmdUtil.execShellCmd("input keyevent 3");//KEYCODE_HOME
    }

    /**
     * 执行锁屏命令
     */
    public static void execLockScreenCmd(){
        new AsyncTask<Void, Void, Boolean>() {
            @Override
            protected Boolean doInBackground(Void... params) {
                //锁屏
                CmdUtil.execShellCmd("input keyevent 3");//KEYCODE_HOME
                try {
                    Thread.sleep(1000*3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                CmdUtil.execShellCmd("input keyevent 26");//KEYCODE_POWER
                return true;
            }

            @Override
            protected void onPostExecute(Boolean success) {

            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    /**
     * 执行滑屏命令
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     */
    public static void execSwipeScreenCmd(final int x1,final int y1,final int x2,final int y2){
        new AsyncTask<Void, Void, Boolean>() {
            @Override
            protected Boolean doInBackground(Void... params) {
                CmdUtil.execShellCmd("input swipe "+x1+" "+y1+" "+x2+" "+y2+"");//下一页
                return true;
            }
            @Override
            protected void onPostExecute(Boolean success) {

            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    /**
     * 执行解锁命令
     */
    public static void execUnClockDeviceCmd(final Application application, final CmdListener cmdListener) {
        new AsyncTask<Void, Void, Boolean>() {
            @Override
            protected Boolean doInBackground(Void... params) {
                try {
                    if(!AppUtils.isScreenOn(application)) {//屏幕熄灭状态
                        CmdUtil.execShellCmd("input keyevent 26");//KEYCODE_POWER // 点亮屏幕
                        Thread.sleep(1000*2);
                    }

                    if (AppUtils.isLockDevice(application)) {//锁屏状态
                        //执行滑屏解锁动作
                        CmdUtil.execShellCmd("input swipe "+AppUtils.getScreenWith()/2+" "
                                +AppUtils.getScreenHeight()*2/3+" "+AppUtils.getScreenWith()/2+" "
                                +AppUtils.getScreenHeight()*1/3+"");//上一页
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return true;
            }
            @Override
            protected void onPostExecute(Boolean success) {
                if (cmdListener != null){
                    cmdListener.onResult("success");
                }
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    public interface CmdListener{
        public void onResult(String result);
    }

}
